package com.bukaedu.info.manager.controller;

import com.bukaedu.info.manager.domain.Student;
import com.bukaedu.info.manager.service.StudentService;

import java.util.Scanner;

public class StudentController {

    private Scanner sc = new Scanner(System.in);
    private StudentService studentService = new StudentService();

    public void start() {
        // 进入学生管理系统
        studentLoop:
        while (true){
            System.out.println("---------欢迎来到<学生>管理系统---------");
            System.out.println("请输入您的选择：\n1.添加学生\n2.删除学生\n3.修改学生\n4.查看学生\n5.退出");
            String choice = sc.next();
            switch (choice){
                case "1":
//                    System.out.println("添加学生");
                    addStudent();
                    break ;
                case "2":
//                    System.out.println("删除学生");
                    deleteStudentById();
                    break;
                case "3":
//                    System.out.println("修改学生");
                    updateStudent();
                    break ;
                case "4":
//                    System.out.println("查看学生");
                    findAllStudent();
                    break ;
                case "5":
                    System.out.println("感谢使用<学生>管理，再见！");
                    break studentLoop;
                default:
                    System.out.println("你的指令有误，请重新输入！");
                    break ;
            }
        }

    }

    private void deleteStudentById() {
        String delId = inputStudentId();
        // 去删除
        studentService.deleteStudentById(delId);

            System.out.println("删除成功！");
    }

    private void updateStudent() {
        String updateId = inputStudentId();
        Student newStu = inputStudentInfo(updateId);
        studentService.updateStudent(updateId,newStu);
        System.out.println("修改成功！");
    }

    private void addStudent() {
        String id ;
        while(true){
            System.out.println("请输入学生id:");
            id  = sc.next();
            // flag  true 代表id存在（被占用）
            boolean flag = studentService.isExists(id);
            if(flag){
                System.out.println("学号被占用！请重新输入！");
            }else{
//                System.out.println("学号可用，");
                break;
            }
        }
        Student stu = inputStudentInfo(id);
        boolean result = studentService.addStudent(stu);
        if(result){
            System.out.println("添加成功！");
        }else{
            System.out.println("添加失败！数据库满啦！");
        }
    }

    public void findAllStudent() {
        Student[] stus = studentService.findAllStudent();
        if(stus == null){
            System.out.println("系统内暂无数据，快去添加~");
            return;
        }
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < stus.length; i++) {
            Student stu = stus[i];
            if(stu != null){
                System.out.println(stu.getId() + "\t" +stu.getName() +"\t" + stu.getAge() + "\t\t" + stu.getBirthday());
            }
        }
    }

    // 实现方法
    public String inputStudentId(){
        String id;
        while(true){
            System.out.println("请输入学生ID：");
            id = sc.next();
            // 校验id是否存在
            boolean exists = studentService.isExists(id);
            if(!exists){
                System.out.println("您输入的id不存在,请重新输入！");
//                break;
            }else{
                break;
            }
        }
        return id;
    }

    public Student inputStudentInfo(String id){
        System.out.println("请输入学生姓名：");
        String name = sc.next();
        System.out.println("请输入学生年龄：");
        String age = sc.next();
        System.out.println("请输入学生生日：");
        String birthday = sc.next();
        Student stu = new Student();
        stu.setName(name);
        stu.setAge(age);
        stu.setBirthday(birthday);
        stu.setId(id);
        return  stu;
    }
}
